/**
 * 
 */
package cn.stylefeng.guns.sys.core.util.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author likangli
 * @2018年11月22日 日期工具类
 */
public class DateUtils {

	/**
	 * 为指定日期加上指定时间值
	 * 
	 * @param date  指定时间
	 * @param time  要加上的时间
	 * @param field 要添加的时间段 如 Calendar.DAY_OF_YEAR
	 * @return
	 */
	public static Date addTime(Date date, Integer time, Integer field) {

		Calendar instance = Calendar.getInstance();
		instance.setTime(date);
		instance.add(field, time);

		return instance.getTime();
	}

	/**
	 * 比较date1 和 date2 是否在同一天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean equels(Date date1, Date date2) {

		if (null == date1 || null == date2)
			return false;

		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();

		c1.setTime(date1);
		c2.setTime(date2);

		if (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR)) {

			if (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH)) {

				if (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH))
					return true;
			}

		}

		return false;
	}

	/**
	 * 获取当天的后一天日期
	 */
	public static Date getAfterDay() {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = sdf.format(new Date());

		Date date = null;
		try {
			date = sdf.parse(currentDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);
		;
		c1.add(Calendar.DAY_OF_MONTH, 1);

		return c1.getTime();

	}

	/**
	 * 判断当前是否是月初
	 */
	public static boolean isBeginMonth() {

		Calendar c1 = Calendar.getInstance();
		c1.setTime(new Date());

		if (c1.get(Calendar.DAY_OF_MONTH) == 1)
			return true;

		return false;

	}

	/**
	 * 获取时间戳 秒
	 */
	public static long generateTimestamp() {

		return new Date().getTime() / 1000;

	}

	/**
	 * @return 获取某个日期距离现在多久了
	 */
	public static String getTimeDist(Date date) {

		Calendar newTime = Calendar.getInstance();
		newTime.setTime(new Date());
		Calendar dTime = Calendar.getInstance();
		dTime.setTime(date);

		if (newTime.get(Calendar.YEAR) != dTime.get(Calendar.YEAR)) {
			return "一年前";
		} else if (newTime.get(Calendar.MONTH) != dTime.get(Calendar.MONTH)) {

			return newTime.get(Calendar.MONTH) - dTime.get(Calendar.MONTH) + "月前";
		} else if (newTime.get(Calendar.DAY_OF_MONTH) != dTime.get(Calendar.DAY_OF_MONTH)) {

			return newTime.get(Calendar.DAY_OF_MONTH) - dTime.get(Calendar.DAY_OF_MONTH) + "日前";
		} else if (newTime.get(Calendar.HOUR_OF_DAY) != dTime.get(Calendar.HOUR_OF_DAY)) {

			return newTime.get(Calendar.HOUR_OF_DAY) - dTime.get(Calendar.HOUR_OF_DAY) + "小时前";
		} else {

			int dist = newTime.get(Calendar.MINUTE) - dTime.get(Calendar.MINUTE);
			if (0 == dist)
				return "刚刚";

			return dist + "分钟前";
		}

	}

	/**
	 * @param time
	 * @return 解析字符串为时间 解析失败返回当前时间
	 */
	public static Date getDateByString(String time, String format) {

		Date newDate = new Date();

		if (StringUtils.isEmpty(time))
			return newDate;

		SimpleDateFormat sdf = new SimpleDateFormat(format);

		try {
			newDate = sdf.parse(time);

		} catch (ParseException e) {
			newDate = new Date();
		}

		return newDate;
	}
	/**
	 * @param time
	 * @return 解析字符串为时间 解析失败返回当前时间
	 */
	public static String getStringByDate(Date time, String format) {

		Date newDate = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(format);

		if (null == time)
			return sdf.format(newDate);

		return sdf.format(time);
	}

	/**
	 * 获取距离晚上十二点的秒数
	 * 
	 * @return
	 */
	public static int getDist24Second() {

		Calendar newTime = Calendar.getInstance();
		newTime.setTime(new Date());

		int hour = newTime.get(Calendar.HOUR_OF_DAY);
		int minute = newTime.get(Calendar.MINUTE);
		int second = newTime.get(Calendar.SECOND);

		int day = 24 * 60 * 60;
		int current = hour * 60 * 60 + minute * 60 + second;

		return day - current;
	}

	/**
	 * 获取另个日期的天数差距
	 * 同一天返回 0
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static int getDistDay(Date beginDate, Date endDate) {

		Calendar beginCalendar = Calendar.getInstance();
		beginCalendar.setTime(beginDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		long beginTime = beginCalendar.getTime().getTime();
		long endTime = endCalendar.getTime().getTime();
		int betweenDays = (int) ((endTime - beginTime) / (1000 * 60 * 60 * 24));// 先算出两时间的毫秒数之差大于一天的天数

		endCalendar.add(Calendar.DAY_OF_MONTH, - betweenDays);// 使endCalendar减去这些天数，将问题转换为两时间的毫秒数之差不足一天的情况
		endCalendar.add(Calendar.DAY_OF_MONTH, -1);// 再使endCalendar减去1天
		if (beginCalendar.get(Calendar.DAY_OF_MONTH) == endCalendar.get(Calendar.DAY_OF_MONTH))// 比较两日期的DAY_OF_MONTH是否相等
			return betweenDays + 1; // 相等说明确实跨天了
		else
			return betweenDays + 0; // 不相等说明确实未跨天

	}

}
